<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Article;
use App\Models\ArticleCounts;

class ArticleController extends Controller
{
    public function index(Request $request){
        # 分页数据
        $data = Article::select(["id","pic","title","desn"])->paginate(config("app.pagsize"));
        return ["status" => 0,"msg" => "获取成功","data" => $data];
    }
    # 资讯详情
    public function show(Article $article){
        return ["status" => "0","msg" => "获取成功" ,"data" => $article];
    }
    # 资讯访问
    public function count(Request $request){
        $art_id = $request->get("art_id");
        $rid = $request->get("rid");
        # 获取当前时间
        $vdt = date("Y-m-d");
        # 让文章的访问自增长1
        Article::where("id",$art_id)->increment('click', 1);
        $ret = ArticleCounts::where("rid",$rid)->where("art_id",$art_id)->where("vdt",$vdt)->first();
        if(!$ret){ # 没有记录
            # 添加
            $ret = ArticleCounts::create([
                "rid" => $rid,
                "art_id" => $art_id,
                "vdt" => $vdt,
                "click" => 1
            ]);
        }else{
            # 修改
            $ret->increment('click');
        }
        return ["status" => 0,"msg" => "成功" , "data" => $ret->click];
    }
}
